﻿using DbJobsApi.Extensions;
using Newtonsoft.Json;
using System.Linq;

namespace DbJobsApi.Dto.SqlServer
{
    public class SearchTableEntity : PageBase
    {
        public SearchTableEntity()
        {
            sidx = "TABLE_NAME";
            sord = "ASC";
            rows = 20;
            page = 1;
        }
        public string DbConnStr { get; set; }
        public string? DbName { get; set; }
        public string? TABLE_NAME { get; set; }
    }
    public class GetTableEntity
    {
        public string DbConnStr { get; set; }
        public string DbName { get; set; }
        public string TABLE_NAME { get; set; }
    }

    public class GetMetaEntity
    {
        public string DbConnStr { get; set; }
        public string DbName { get; set; }
        public string Type { get; set; }
        public string Name { get; set; }
    }

    public class CustomTableEntity
    {
        public string DbConnStr { get; set; }
        /// <summary>
        /// 库名
        /// </summary>
        public string DbName { get; set; }
        /// <summary>
        /// 表名(原始值，替代id)
        /// </summary>
        public string EDIT_TABLE_NAME { get; set; }
        /// <summary>
        /// 表名
        /// </summary>
        public string TABLE_NAME { get; set; }
        public string? TABLE_TYPE { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string COMMENTS { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CREATED { get; set; }
        /// <summary>
        /// 列
        /// </summary>
        public List<TableColumnEntity> Columns { get; set; }
    }

    public class TableColumnEntity
    {
        /// <summary>
        /// 是否键
        /// </summary>
        public int ISKEY { get; set; }
        public int AUTOIN { get; set; }
        /// <summary>
        /// 主键名
        /// </summary>
        public string? CONSTRAINT_NAME { get; set; }
        /// <summary>
        /// 列名(原始值，替代id)
        /// </summary>
        public string EDIT_COLUMN_NAME { get; set; }
        /// <summary>
        /// 列名
        /// </summary>
        public string COLUMN_NAME { get; set; }

        [Newtonsoft.Json.JsonIgnore]
        public string COLUMN_NAME_SQL 
        { 
            get
            {
                if (string.IsNullOrEmpty(DATA_TYPE))
                    return string.Empty;

                string sizeDefine = string.Empty;
                if (DATA_LENGTH.HasValue && DATA_SCALE.HasValue)
                    sizeDefine = $"({DATA_LENGTH},{DATA_SCALE})";
                else if (DATA_LENGTH.HasValue && !DATA_SCALE.HasValue)
                    sizeDefine = $"({DATA_LENGTH})";

                var size = $"{DATA_TYPE}{ sizeDefine }";
                var autoin = AUTOIN == 1 ? "IDENTITY(1,1)" : String.Empty;
                return $"\"{COLUMN_NAME}\" {size} {autoin} {DATA_DEFAULT_SQL} { (NULLABLE == "N" ? "NOT NULL" : "") }";
            } 
        }
        /// <summary>
        /// 类型
        /// </summary>
        public string DATA_TYPE { get; set; }
        /// <summary>
        /// 长度, NUMBER类型时无须填写, 固定22
        /// </summary>
        public int? DATA_LENGTH { get; set; }
        /// <summary>
        /// 小数位
        /// </summary>
        public int? DATA_SCALE { get; set; }
        /// <summary>
        /// 默认值
        /// </summary>
        public string? DATA_DEFAULT { get; set; }
        [Newtonsoft.Json.JsonIgnore]
        public string DATA_DEFAULT_SQL
        { 
            get
            {
                if (string.IsNullOrEmpty(DATA_DEFAULT))
                    return string.Empty;
                else
                    return $"DEFAULT {DATA_DEFAULT}";
            } 
        }
        /// <summary>
        /// 可空, Y/N
        /// </summary>
        public string NULLABLE { get; set; }
        /// <summary>
        /// 字段备注
        /// </summary>
        public string? COMMENTS { get; set; }
    }


    public class SearchEntity : PageBase
    {
        public SearchEntity()
        {
            sidx = "ID";
            sord = "ASC";
            rows = 50;
            page = 1;
        }
        public string DbConnStr { get; set; }
        [JsonConverter(typeof(RegexStringConverter), "^[A-Za-z0-9_]+$")]
        public string DbName { get; set; }
        [JsonConverter(typeof(RegexStringConverter), "^[A-Za-z0-9_]+$")]
        public string TABLE_NAME { get; set; }
        public string Filter { get; set; }
    }

    public class RowImportEntity : GetTableEntity
    {
        public List<string> Fields { get; set; }
        public List<object[]> Rows { get; set; }
    }

    public class RunSqlEntity
    {
        public string DbConnStr { get; set; }
        public string[] Sqls { get; set; }
    }
}
